package com.d3works.apps.stock.service;

import com.d3works.apps.base.service.AddressServiceImpl;
import com.d3works.apps.stock.db.StockLocation;
import com.d3works.apps.stock.db.StockMove;
import com.d3works.db.JPA;

public class AddressServiceStockImpl extends AddressServiceImpl {
  static {
    registerCheckUsedFunc(AddressServiceStockImpl::checkAddressUsedStock);
  }

  private static boolean checkAddressUsedStock(Long addressId) {
    return JPA.all(StockMove.class)
                .filter("self.fromAddress.id = ?1 OR self.toAddress.id = ?1", addressId)
                .fetchOne()
            != null
        || JPA.all(StockLocation.class).filter("self.address.id = ?1", addressId).fetchOne()
            != null;
  }
}
